package com.xiaoxu.intranetweb.mybatisplusSzappdb.controller.weightSheets;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSONObject;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.bo.WeightPicturesSamplingBo;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.business.weightSheets.WeightSheetsBu;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.dao.UserInfoDao;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.CWeightPicturesSampling;
import com.xiaoxu.intranetweb.util.PermissionCheck;
import com.xiaoxu.intranetweb.util.SzResult;
import com.xiaoxu.intranetweb.util.WebFunctionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/weightSheets")
public class WeightSheetsController {
    @Autowired
    private WeightSheetsBu weightSheetsBu;
    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private WebFunctionUtil webFunctionUtil;

    //    删除磅单
    @PostMapping("/deleteWeightSheets")
    @PermissionCheck("weightSheets_delete")
    public SzResult deleteWeightSheets(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String id = jsonObject.getString("id");
        String reason = jsonObject.getString("reason");
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return weightSheetsBu.deleteWeightSheets(id, StpUtil.getLoginIdByToken(token).toString(), reason, token, plantId);
    }

    //单据调价String price, List<String> weishtSheetnos, String userId
    @PostMapping("/updateWeightSheetsPrice")
    @PermissionCheck("weightSheets_updatePrice")
    public SzResult updateWeightSheetsPrice(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
//        bigDecimal price, List<String> weishtSheetnos, String userId
        String price = jsonObject.getString("price");
        List<String> weishtSheetnos = jsonObject.getJSONArray("weishtSheetnos").toJavaList(String.class);
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return weightSheetsBu.updateWeightSheetsPrice(price, weishtSheetnos, StpUtil.getLoginIdByToken(token).toString(), plantId);
    }

    @PostMapping("/addPipeAshWeightSheet")
    public SzResult addPipeAshWeightSheet(@RequestBody JSONObject jsonObject, HttpServletRequest request){
        String weight = jsonObject.getString("weight");//重量
        String endTime = jsonObject.getString("endTime");//结束时间
        String token = request.getHeader("Authorization");
        String plantId = jsonObject.getString("plantId");
        return weightSheetsBu.addPipeAshWeightSheet(weight, endTime, plantId);
    }

    //单据一键调价
    @PostMapping("/updateWeightSheetsPriceAll")
    @PermissionCheck("weightSheets_updatePrice")
    public SzResult updateWeightSheetsPriceAll(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
//        String price, String userId,String fullTimeStart, String fullTimeEnd,
//                                               String coSupply, String coSupplyChi, String co, String chiCo, List<String> product, String truckno, String operator, String sheetno, String reason, String iccardno, Integer isEmpty
        String price = jsonObject.getString("price");
        String fullTimeStart = jsonObject.getString("fullTimeStart");
        String fullTimeEnd = jsonObject.getString("fullTimeEnd");
        String coSupply = jsonObject.getString("coSupply");
        String coSupplyChi = jsonObject.getString("coSupplyChi");
        String co = jsonObject.getString("co");
        String chiCo = jsonObject.getString("chiCo");
        if (coSupply !=null && coSupply.contains(".")) {
            String[] coSupplyList = coSupply.split("\\.");

            coSupply = coSupplyList[0];
            coSupplyChi = coSupplyList[1];
        }else {

        }
        if (co !=null && co.contains(".")) {
            String[] coList = co.split("\\.");

            chiCo = coList[1];
            co = coList[0];
        }
//        product不为空的时候
//        List<String> product = null;
//        if (jsonObject.getJSONArray("product") !=null && jsonObject.getJSONArray("product").size()>0){
//          product = jsonObject.getJSONArray("product").toJavaList(String.class);
//        }else {
//           product = null;
//        }
        String product = jsonObject.getString("product");
        String truckno = "";
        if (jsonObject.getJSONArray("truckno") != null && jsonObject.getString("truckno").length() > 0) {
            truckno = jsonObject.getString("truckno");
        } else {
            truckno = null;
        }
        String operator = jsonObject.getString("operator");
        String sheetno = jsonObject.getString("sheetno");
        String reason = jsonObject.getString("reason");
        String iccardno = jsonObject.getString("iccardno");
        Integer isEmpty = jsonObject.getInteger("isEmpty");
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return weightSheetsBu.updateWeightSheetsPriceAll(price, StpUtil.getLoginIdByToken(token).toString(), fullTimeStart, fullTimeEnd, coSupply, coSupplyChi, co, chiCo, product, truckno, operator, sheetno, reason, iccardno, isEmpty, plantId);
    }


    //退换货
    @GetMapping("/pdtReturnOrExchange")
    @PermissionCheck("weightSheets_pdtReturnOrExchange")
    public SzResult pdtReturnOrExchange(@RequestParam String weishtSheetNo, @RequestParam Integer ReturnOrExchange, @RequestParam(required = false) String exchangePdtId, @RequestParam(required = false) String reason, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return weightSheetsBu.pdtReturnOrExchange(weishtSheetNo, ReturnOrExchange, exchangePdtId, reason, token, plantId);
    }


    /**
     * 模板导表
     */


    @GetMapping("/templateExport")
    public SzResult templateExport(@RequestParam String id, @RequestParam String templateId, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
  /*      if (!userInfoDao.getUserPermissionWeb(StpUtil.getLoginIdByToken(token).toString()).containsKey("WeightSheets_templateExport")) {
            return SzResult.error("您没有模板导表的权限");
        }*/

        return weightSheetsBu.templateExport(id, templateId, token);
    }


    /**
     * 磅单得高级操作
     */

    @PostMapping("/weightSheetAdvanced")
    @PermissionCheck("weightSheets_advanced")

    public SzResult weightSheetAdvanced(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {

        //String id, String truckNo, String product, String tranChiCo, String weishtGrossWght, String weishtTare, String weishtNetWght, String weishtMoney,
        //                                        String supplyCo, String supplyChiCo, String co, String chiCo, String weishtEmptyTm, String weishtFullTm, String token) throws Exception {
        String id = jsonObject.getString("id");
        String truckNo = jsonObject.getString("truckNo");
        String product = jsonObject.getString("product");
        String tranChiCo = jsonObject.getString("tranChiCo");
        String weishtGrossWght = jsonObject.getString("weishtGrossWght");
        String weishtTare = jsonObject.getString("weishtTare");
        String weishtNetWght = jsonObject.getString("weishtNetWght");
        String weishtMoney = jsonObject.getString("weishtMoney");
        String supplyCo = jsonObject.getString("supplyCo");
        String supplyChiCo = jsonObject.getString("supplyChiCo");
        String co = jsonObject.getString("co");
        String chiCo = jsonObject.getString("chiCo");
        String weishtEmptyTm = jsonObject.getString("weishtEmptyTm");
        String weishtFullTm = jsonObject.getString("weishtFullTm");
        String weishtLoadTm = jsonObject.getString("weishtLoadTm");
        String weishtLoadEndTm = jsonObject.getString("weishtLoadEndTm");
        String weishtEnterTm = jsonObject.getString("weishtEnterTm");
        String weishtOutTm = jsonObject.getString("weishtOutTm");
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return weightSheetsBu.weightSheetAdvanced(id, truckNo, product, tranChiCo, weishtGrossWght, weishtTare, weishtNetWght, weishtMoney, supplyCo, supplyChiCo, co, chiCo, weishtEmptyTm, weishtFullTm, weishtLoadTm, weishtLoadEndTm, weishtEnterTm, weishtOutTm, token, plantId);
    }

    /**
     * 磅单打印
     */
    @PostMapping("/weightSheetPrint")
    public SzResult weightSheetPrint(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        List<String> weightSheetIdList = jsonObject.getJSONArray("weightSheetIdList").toJavaList(String.class);
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return weightSheetsBu.weightSheetPrint(weightSheetIdList, token, plantId);
    }

    //年度环比报表
    @PostMapping("/yearOnYearReport")
    public SzResult yearOnYearReport(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        Integer year1 = jsonObject.getInteger("year1");
        Integer year2 = jsonObject.getInteger("year2");
        String cpyId = jsonObject.getString("cpyId");
        return weightSheetsBu.yearOnYearReport(year1, year2, cpyId, plantId);
    }

    @PostMapping("/yearOnYearReportByPdt")
    public SzResult yearOnYearReportByPdt(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        Integer year1 = jsonObject.getInteger("year1");
        Integer year2 = jsonObject.getInteger("year2");
        String pdtId = jsonObject.getString("pdtId");
        return weightSheetsBu.yearOnYearReportByPdt(year1, year2, pdtId, plantId);
    }
//        public SzResult getWeightSheetImg(String weightSheetsNo) {
    @PostMapping("/getWeightSheetImg")
    public SzResult getWeightSheetImg(@RequestBody JSONObject jsonObject) {
        String weightSheetsNo = jsonObject.getString("weightSheetsNo");
        return weightSheetsBu.getWeightSheetImg(weightSheetsNo);
    }

    /**
     * 查看采样图片上传
     */
    @GetMapping("/getSamplingImg")
    public SzResult getSamplingImg(@RequestParam String weipicSheetno) throws Exception {
        return weightSheetsBu.getSamplingImg(weipicSheetno);
    }

    /**
     * 磅单采样图片上传
     */
    @PostMapping("/samplingUpload")
    public SzResult samplingUpload(@RequestBody WeightPicturesSamplingBo weightPicturesSamplingBo) throws Exception {
         weightSheetsBu.samplingUpload(weightPicturesSamplingBo);
         return SzResult.success("上传成功");
    }
    /**
     * 磅单采样图片上传 测试
     */
    @GetMapping ("/samplingUploadTest")
    public SzResult samplingUploadtest(){
        return SzResult.success("OK");
    }

//    //    重置downId11为0
//    public void resetDownId11(String id) {
    @PostMapping("/resetDownId11")
    public SzResult resetDownId11(@RequestBody JSONObject jsonObject) {
        String id = jsonObject.getString("id");
        weightSheetsBu.resetDownId11(id);
        return SzResult.success("重置成功");
    }
}
